const distanceX = 685;  //1457,1280
const distanceY = 480; 
const firstCarX = 775;  //773，802
const firstCarY = 805; 

var curCarNum = 8;

const carNums = [1, 2, 3, 4, 5, 6, 8];

mainEntrence();

// 程序主入口
function mainEntrence() {
  deviceInfo();
  eventListener();

  sleep(800);

  // 选关卡
  //beforeRun();
  //while (true) {
  // 运行主函数
  main();
  //}
}


function eventListener() {
  threads.start(function () {
    // 启用按键监听
    events.observeKey();
    // 监听音量上键按下
    events.onKeyDown("volume_down", function (event) {
      toastLog("程序手动退出");
      threads.shutDownAll();
      exit();
    });
  });
}


function deviceInfo() {
  auto();
  if (!requestScreenCapture()) {
    toast('请求截图失败，程序结束');
    exit();
  }
  // 调整屏幕亮度
  device.setBrightness(0);
}

function main() {
  toast("开始运行");

  career();

  // 跑完之后

}


function career(){
  click(1050,535);
  sleep(1000);
  clickContinue();
  sleep(3000);

  chooseCar();
  checkTouchDrive();

  // 开始
  click(2400, 1280);

  sleep(5000);

  // 开跑
  runUtilEnd();
  
}

function chooseCar(){
  var img = captureScreen();
  var selected = false;

  for(i=0;i<carNums.length;i++){
    var num = carNums[i];
    selected = selectCar(img,firstCarX + parseInt((num - 1) / 2) * distanceX,firstCarY + ((num - 1) % 2) * distanceY);
    if(selected){
      toast("选车完成");
      sleep(2000);
      return
    }
  }

  stop();
}

function selectCar(img,x,y){
  var oil = images.pixel(img, checkForResolution(x, y, img).positionX, checkForResolution(x, y, img).positionY);
  if (colors.isSimilar(oil, "#ffc0f814",10)) {
    click(x, y);
    return true;
  }
  return false;
}

function checkTouchDrive() {
  var img = captureScreen();
  sleep(1000);
  var tdPoint = images.findColor(img, "#ffc0f814", 2700, 1060, 120, 100, 8);
  if (tdPoint == null) {
      toast("打开自动驾驶");
      click(2700, 1100);
      sleep(2000);
  }
}

function afterRun() {
  toast("跑完了");
  sleep(1000);
  clickContinue();
  sleep(1500);
  // var img = captureScreen();
  // var continueBtn = images.pixel(img, checkForResolution(2280, 1260, img).positionX, checkForResolution(2280, 1260, img).positionY);
  // if (colors.isSimilar(continueBtn, "#ffc3fb12")) {
    clickContinue();
    sleep(3500);
    clickContinue();
    sleep(1500);
    clickContinue();
    sleep(5000);

    var startPage = false;
    do{
      sleep(2000);
      var back = getPointColor(captureScreen(),100,100);
      startPage = colors.equals(back,"#ffffffff");
    }while(!startPage);
    career();

}

function runUtilEnd() {
  toast("开始");
  var isEnd = false;
  do {
      sleep(1000);
      var continueBtn = getPointColor(captureScreen(), 2380, 1260);
      if (colors.isSimilar(continueBtn, "#ffc3fb12")) {
          isEnd = true;
      } else {
          swipe(1960, 720, 1000, 720, 500);
          click(2490, 1120);
      }
  } while (!isEnd);
  afterRun();
}

function clickContinue() {
  click(2280, 1260);
}

function isRunStart(){
  var img = captureScreen();
  var timeClock = images.pixel(img, checkForResolution(2430, 220, img).positionX, checkForResolution(2430, 220, img).positionY);
  isStarted = colors.isSimilar(timeClock,"#ffd90d52");
  
}

function checkForResolution(x, y, img) {
  var width = img.getWidth();
  var height = img.getHeight();
  if (width < height) {
    var e = x;
    x = y;
    y = e;
  }

  return {
    positionX: x,
    positionY: y
  };
}

function getPointColor(img, x, y) {
  var data = checkForResolution(x, y, img);
  return images.pixel(img, data.positionX, data.positionY);
}